package view;


import util.Controller;
import util.MusicPlayer;

import javax.swing.*;
import java.awt.*;

public class Plant extends JLabel implements Runnable{
    private Image offScreenImage;  //图形缓存
    private ImageIcon img;
    private int r;
    private int c;
    private int space;
    private int hp;
    private int pic;
    private int sumPic;
    private int price;
    private boolean isAlive;
    private int strength;
    private String name;
    private Controller controller;

    public void setImg(ImageIcon img) {
        this.img = img;
    }

    public void setController(Controller controller) {
        this.controller = controller;
    }

    public void setR(int r) {
        this.r = r;
    }

    public int getR() {
        return r;
    }

    public void setC(int c) {
        this.c = c;
    }

    public int getC() {
        return c;
    }

    public int getHp() {
        return hp;
    }

    public int getPic() {
        return pic;
    }

    public int getSumPic() {
        return sumPic;
    }

    public int getPrice() {
        return price;
    }

    public String getName() {
        return name;
    }

    public Plant getPlant(String name){
        Plant newPlant = null;
        switch (name){
            case "SunFlower"    :   newPlant = new SunFlower();     break;
            case "Peashooter"   :   newPlant = new Peashooter();    break;
            case "CherryBomb"   :   newPlant = new CherryBomb();    break;
            case "PotatoMine"   :   newPlant = new PotatoMine();    break;
            case "WallNut"      :   newPlant = new WallNut();       break;
            case "WallNutRoll"  :   newPlant = new WallNutRoll();   break;
            case "Repeater"  :   newPlant = new Repeater();   break;
        }
        return newPlant;
    }

    public void setSpace(int space) {
        this.space = space;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public void setPic(int pic) {
        this.pic = pic;
    }

    public void setSumPic(int sumPic) {
        this.sumPic = sumPic;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Plant(){ }

    public Plant(String name, int price, int sumPic){
        this.isAlive = true;
        this.name = name;
        this.price = price;
        this.sumPic = sumPic;
        this.pic = 0;
        this.hp = 6;
        this.strength = 50;
        this.img = new ImageIcon("img\\tt.png");
    }

    public int getIconWidth(){
        return img.getIconWidth();
    }
    public int getIconHeight(){
        return img.getIconHeight();
    }
    void picChange(){
        if (pic<sumPic)
            pic++;
        else
            pic = 0;
        img = new ImageIcon("img\\"+this.name+"\\"+this.name+"_"+pic+".png");
    }

    void attack(){
        //生成子弹
    }



    public boolean isAlive() {
        return isAlive;
    }

    void plant(){

    }

    public Controller getController() {
        return controller;
    }

    public int getStrength() {
        return strength;
    }

    public void setStrength(int strength) {
        this.strength = strength;
    }

    public void rest() {
        this.strength = 0;
    }



    void getHurt(){
        //受伤闪动
        this.hp --;
    }

    void die(){
        controller.getPlants()[getR()][getC()] = null;
        this.isAlive = false;
        this.setVisible(false);
        //线程结束
    }

    public void gatherStrength(){  //装载技能
        this.strength ++;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        ImageIcon imgShadow = new ImageIcon("img\\shadow.png");
        g2.drawImage(imgShadow.getImage(), -11, 45,
                imgShadow.getIconWidth(), imgShadow.getIconHeight(), null);
        g.drawImage(img.getImage(), 0, 0,
                img.getIconWidth(), img.getIconHeight(),  null);
    }
    public void update(Graphics g) {
        if(offScreenImage == null)
            offScreenImage = this.createImage(800, 600);
        Graphics gImage = offScreenImage.getGraphics();
        paint(gImage);
        g.drawImage(offScreenImage, 0, 0, this);
    }

    @Override
    public void run(){
        for (int i=1 ; this.isAlive ; i++){
            try {
                Thread.sleep(space);
                this.picChange();
                this.repaint();
                this.gatherStrength();
                if (controller.findZombie(getR(), getC())){
                    this.attack();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class SunFlower extends Plant{
    SunFlower(){
        super("SunFlower", 50, 17);
        this.setSpace(70);
    }
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        ImageIcon img = new ImageIcon("img\\shadow.png");
        g2.drawImage(img.getImage(), -11, 45,
                img.getIconWidth(), img.getIconHeight(), null);
        img = new ImageIcon(
                "img\\"+getName()+"\\"+getName()+"_"+getPic()+".png");
        g.drawImage(img.getImage(), 0, 0,
                img.getIconWidth(), img.getIconHeight(),  null);
        if (getStrength()>165){
            img = new ImageIcon(
                    "img\\Golden"+getName()+"\\"+"Frame"+getPic()+".png");
            g.drawImage(img.getImage(), 0, 0,
                    img.getIconWidth(), img.getIconHeight(),  null);
        }
    }
    public void gatherStrength(){
        setStrength(getStrength()+1);
        if (this.getStrength()>190) {
            new Thread(new Sun(getController(), getR(), getC())).start();
            this.rest();
        }
    }
}

class WallNutRoll extends Plant implements Runnable{
    WallNutRoll(){
        super("WallNutRoll", 0, 7);
    }
    public void run(){
        int x = this.getX();
        int y = this.getY();
        int vy = 0;
        for (int i=1 ; x<=800 ; i++){
            try {
                Thread.sleep(25);
                if (i%3==0)
                    this.picChange();
                this.repaint();
                x += 6;
                y += vy;
                this.setBounds(x, y, this.getWidth(), this.getHeight());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.setHp(0);
    }
    void attack(){

    }
}
class WallNut extends Plant{
    WallNut(){
        super("WallNut", 50, 15);
        this.setHp(72);
        this.setSpace(90);
    }
    void getHurt(){
        this.setHp(this.getHp()-1);
        if (this.getHp()==48) {
            this.setName("Wallnut_cracked1");
            this.setPic(0);
            this.setSumPic(10);
        }
        else if (this.getHp()==24) {
            this.setName("Wallnut_cracked2");
            this.setPic(0);
            this.setSumPic(14);
        }
    }
}

class Peashooter extends Plant{
    Peashooter() {
        super("Peashooter", 100, 12);
        this.setSpace(80);
    }
    public void attack(){
        if (this.getStrength()>15 ){
            //生成子弹
            new Thread(new Bullet(getR(), getC(), getController())).start();
            this.rest();
        }
    }
}

class CherryBomb extends Plant{
    CherryBomb() {
        super("CherryBomb", 150, 6);
        this.setSpace(150);
    }
    @Override
    public void run(){
        for (int i=1 ; i<=6 ; i++){
            try {
                Thread.sleep(150);
                this.picChange();
                this.repaint();
                this.gatherStrength();
                this.setBounds(getC()*80+25, getR()*100+90,
                        300, 300 );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        getController().blast(getR(), getC(), 3);   //爆炸
        new Thread(new MusicPlayer("audio\\cherryBomb.mp3")).start();
        this.setImg(new ImageIcon("img\\Boom.png"));
        this.repaint();
        this.setBounds(getC()*80-10, getR()*100+50,
                300, 300 );
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.die();
    }
}

class PotatoMine extends Plant{
    PotatoMine() {
        super("PotatoMine", 25, 20);
    }
}

class Repeater extends Plant{
	Repeater() {
        super("Repeater", 200, 14);
        this.setSpace(80);
    }
    public void attack(){
        if (this.getStrength()>15){
            //生成子弹
            new Thread(new Bullet(getR(), getC(), getController())).start();
            this.rest();
        }
        if (this.getStrength() == 2 ){
            //生成子弹
            new Thread(new Bullet(getR(), getC(), getController())).start();
        }
    }
}